/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Serial;

import slisfx.Globals;

/**
 *
 * @author Everett
 */
public class X10Handler implements Globals {
    
    private static byte[] houseCode = {96, (byte) 224, 32, (byte) 160, 16, 
        (byte)144, 80, (byte) 208, 112, (byte) 240, 48, (byte) 176, 0, 
        (byte) 128, 64, (byte) 192};
    private static byte[] deviceCode = {6, 14, 2, 10, 1, 9, 5, 13, 7, 15, 3, 11,
        0, 8, 4, 12};
    private static byte[] part1x10 = new byte[4];
    private static byte[] part2x10 = new byte[4];
    
    public X10Handler()
    {
        
    }
    
    public void init()
    {
        part1x10[0] = 2;
        part1x10[1] = 99;
        part1x10[3] = 0;
        part2x10[0] = 2;
        part2x10[1] = 99;
        part2x10[3] = (byte) 128;
    }
    
    public void x10on(char house, int device)
    {
        part1x10[2] = (byte) (houseCode[houseToInt(house)] + deviceCode[device-1]);
        part2x10[2] = (byte) (houseCode[houseToInt(house)] + 2);
        
        System.out.println("a "+part1x10[0]);
        plm.write(part1x10);
        try {
            Thread.sleep(500);
        } catch (Exception e) {}
        plm.write(part2x10);
        try {
            Thread.sleep(500); 
        } catch (Exception e) {}
    }
    
    public void x10off(char house, int device)
    {
        part1x10[2] = (byte) (houseCode[houseToInt(house)] + deviceCode[device-1]);
        part2x10[2] = (byte) (houseCode[houseToInt(house)] + 3);
        plm.write(part1x10);
        try {
            Thread.sleep(500);
        } catch (Exception e) {}
        plm.write(part2x10);
        try {
            Thread.sleep(500); 
        } catch (Exception e) {}
    }
    
    public void x10dim(char house, int device)
    {
        part1x10[2] = (byte) (houseCode[houseToInt(house)] + deviceCode[device-1]);
        part2x10[2] = (byte) (houseCode[houseToInt(house)] + 4);
        plm.write(part1x10);
        try {
            Thread.sleep(500);
        } catch (Exception e) {}
        plm.write(part2x10);
        try {
            Thread.sleep(500); 
        } catch (Exception e) {}
    }
    
    public void x10brighten(char house, int device)
    {
        part1x10[2] = (byte) (houseCode[houseToInt(house)] + deviceCode[device-1]);
        part2x10[2] = (byte) (houseCode[houseToInt(house)] + 5);
        plm.write(part1x10);
        try {
            Thread.sleep(500);
        } catch (Exception e) {}
        plm.write(part2x10);
        try {
            Thread.sleep(500); 
        } catch (Exception e) {}
    }
    
    public void plmHandler (byte in[])
    {
        if(in.length==5)
        {
            if(in[0]==0x60 && in[1]==0x63 && in[2]==0x63)
            {
                if(in[3]==0x00)
                {
                    System.out.println("Door triger A is appart");
                }
                else if(in[3]==0x20)
                {
                    System.out.println("Door triger A is together");
                }       
            }
            else if(in[0]==0x00 && in[1]==0x60 && in[2]==0x63)
            {
                if(in[3]==0x00)
                {
                    System.out.println("Door triger B is appart");
                }
                else if(in[3]==0x20)
                {
                    System.out.println("Door triger B is together");
                }       
            }
            else if(in[0]==0xe0 && in[1]==0x68 && in[2]==0x63)
            {
                if(in[3]==0x00)
                {
                    System.out.println("Motion sensor tripped");
                }
                else if(in[3]==0x20)
                {
                    System.out.println("Motion sensor timed out");
                }       
            }
        }
    }
    
    private int houseToInt(char house)
    {
        int houseAsNum=16;
        switch(house){
            case 'A': houseAsNum=0;
                    break;
            case 'B': houseAsNum=1;
                    break;
            case 'C': houseAsNum=2;
                    break;
            case 'D': houseAsNum=3;
                    break;
            case 'E': houseAsNum=4;
                    break;
            case 'F': houseAsNum=5;
                    break;
            case 'G': houseAsNum=6;
                    break;
            case 'H': houseAsNum=7;
                    break;
            case 'I': houseAsNum=8;
                    break;
            case 'J': houseAsNum=9;
                    break;
            case 'K': houseAsNum=10;
                    break;
            case 'L': houseAsNum=11;
                    break;
            case 'M': houseAsNum=12;
                    break;
            case 'N': houseAsNum=13;
                    break;
            case 'O': houseAsNum=14;
                    break;
            case 'P': houseAsNum=15;
                    break;
        }
        return houseAsNum;
                
    }
    
}
